package org.gdstash.character;

import org.gdstash.file.GDReader;
import org.gdstash.file.GDWriter;
import org.gdstash.util.FileVersionException;
import org.gdstash.util.GDMsgFormatter;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class GDCharUISettings {
    public static final int VERSION_4 = 4;
    public static final int VERSION_5 = 5;
    public static final int VERSION_6 = 6;
    public static final int VERSION_7 = 7;
    private static final int BLOCK = 14;
    private int version;
    private byte unknown1;
    private int unknown2;
    private byte unknown3;
    private String[] unknown4;
    private String[] unknown5;
    private byte[] unknown6;
    private int numSkillSet;
    private int slotsPerSet;
    private List<GDcharUISkillSet> skillSets;
    private float cameraDistance;

    public GDCharUISettings() {
        this.skillSets = new LinkedList<GDcharUISkillSet>();
        this.unknown4 = new String[5];
        this.unknown5 = new String[5];
        this.unknown6 = new byte[5];
    }

    public void read() throws IOException {
        int val = 0;
        final GDReader.Block block = new GDReader.Block();
        val = GDReader.readBlockStart(block);
        if (val != 14) {
            throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
        }
        this.version = GDReader.readEncInt(true);
        if (this.version != 4 && this.version != 5 && this.version != 6 && this.version != 7) {
            throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
        }
        this.unknown1 = GDReader.readEncByte();
        this.unknown2 = GDReader.readEncInt(true);
        this.unknown3 = GDReader.readEncByte();
        for (int i = 0; i < this.unknown4.length; ++i) {
            this.unknown4[i] = GDReader.readEncString();
            this.unknown5[i] = GDReader.readEncString();
            this.unknown6[i] = GDReader.readEncByte();
        }
        if (this.version >= 7) {
            this.numSkillSet = GDReader.readEncInt(true);
            this.slotsPerSet = GDReader.readEncInt(true);
        } else {
            this.numSkillSet = 1;
            if (this.version >= 6) {
                this.slotsPerSet = 47;
            } else if (this.version >= 5) {
                this.slotsPerSet = 46;
            } else {
                this.slotsPerSet = 36;
            }
        }
        this.skillSets.clear();
        for (int i = 0; i < this.numSkillSet; ++i) {
            final GDcharUISkillSet skillSet = new GDcharUISkillSet(this.slotsPerSet);
            skillSet.read(this.version);
            this.skillSets.add(skillSet);
        }
        this.cameraDistance = GDReader.readEncFloat(true);
        GDReader.readBlockEnd(block);
    }

    public void write() throws IOException {
        final GDReader.Block block = new GDReader.Block();
        GDWriter.writeBlockStart(block, 14);
        GDWriter.writeInt(this.version);
        GDWriter.writeByte(this.unknown1);
        GDWriter.writeInt(this.unknown2);
        GDWriter.writeByte(this.unknown3);
        for (int i = 0; i < this.unknown4.length; ++i) {
            GDWriter.writeString(this.unknown4[i]);
            GDWriter.writeString(this.unknown5[i]);
            GDWriter.writeByte(this.unknown6[i]);
        }
        if (this.version >= 7) {
            GDWriter.writeInt(this.numSkillSet);
            GDWriter.writeInt(this.slotsPerSet);
        }
        for (final GDcharUISkillSet set : this.skillSets) {
            if (set != null) {
                set.write(this.version);
            }
        }
        GDWriter.writeFloat(this.cameraDistance);
        GDWriter.writeBlockEnd(block);
    }

    public int getByteSize() {
        int size = 0;
        size += 4;
        size += 4;
        size += 4;
        ++size;
        size += 4;
        ++size;
        for (int i = 0; i < this.unknown4.length; ++i) {
            size += 4;
            if (this.unknown4[i] != null) {
                size += this.unknown4[4].length();
            }
            size += 4;
            if (this.unknown5[i] != null) {
                size += this.unknown5[4].length();
            }
            ++size;
        }
        if (this.version >= 7) {
            size += 4;
            size += 4;
        }
        for (final GDcharUISkillSet set : this.skillSets) {
            if (set != null) {
                size += set.getByteSize(this.version);
            }
        }
        size += 4;
        size += 4;
        return size;
    }
}
